﻿var codeMirrorObject = null;

function handleError(data) {
    window.location = data;
}

$(document).ready(function () {
    $('#describermodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "Close": function () { $(this).dialog("close"); } } });
    $('#describemmodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "Close": function () { $(this).dialog("close"); } } });
    $('#addfiletype').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFileType($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#editfiletype').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { EditFileType($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addpage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddPage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "Close": function () { $(this).dialog("close"); } } });
    $('#addrmodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddRModule($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addmmodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddMModule($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFooter($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addcss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addjs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addleftnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddLeftNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addleftnavitem').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddLeftNavItem($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addtopnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddTopNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addtopnavitem').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddTopNavItem($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#adduser').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddUser($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#edituser').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { EditUser($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletepage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeletePage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFile($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletermodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteRModule($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletemmodule').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteMModule($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deleteheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFooter($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletecss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletejs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamepage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenamePage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameFile($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renameheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamecss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamejs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#permissions').dialog({ autoOpen: false, height: 400, width: 700, modal: true, buttons: { "Close": function () { $(this).dialog("close"); return false; } } });
    LoadDirectory();
    InitializeUploadify();
});

function InitializeUploadify() {
    $('#fileNew').uploadify({
        'uploader': '/cms/flash/uploadify.swf',
        'script': '/lidocaine.ashx?f=Upload',
        'wmode': 'transparent',
        'multi': true,
        'cancelImg': '/cms/images/uploadify_cancel.png',
        'auto': false,
        'queueSizeLimit': 100,
        'sizeLimit': 400000000,
        'buttonText': 'Choose Files',
        'onSelectOnce': function () {
            var cookie = $.cookie('Lidocaine');
            var checked = "no";
            if ($('#NewFileUnzipZips').attr('checked')) {
                checked = "yes";
            }
            var path = $('#path').val();
            $('#fileNew').uploadifySettings('scriptData', { 'strFilepath': path, 'strCookie': cookie, 'unzip': checked });
            $('#fileNew').uploadifyUpload();
        },
        'onComplete': function (event, queueID, fileObj, response, data) {
            $("#ulValidation").append("<br />" + response);
            LoadDirectory($('#path').val());
        }
    });
}

function ShowTemplateInterface() {
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowTemplateInterface();return false;\"><span class=\"icon sprite-Settings\"></span>Template Administration</a>");
    $('#explorer').html("<div class=\"schem-wrapper\"><a href=\"#\" onclick=\"LoadHeaders();return false;\" class=\"schem schem-header-top-footer\"><br />HEADER<br /><br /></a><a href=\"#\" onclick=\"LoadCss();return false;\" class=\"schem schem-css-js\"><br />CSS<br /><br /></a><a href=\"#\" onclick=\"LoadJs();return false;\" class=\"schem schem-css-js\"><br />JS<br /><br /></a><a href=\"#\" onclick=\"LoadTopNav();return false;\" class=\"schem schem-header-top-footer\"><br />TOP NAVIGATION<br /><br /></a><div><a href=\"#\" onclick=\"LoadLeftNav();return false;\" class=\"schem schem-left-right\"><br /><br />LEFT<br />NAVIGATION</a><a href=\"#\" onclick=\"LoadMModules();return false;\" class=\"schem schem-main\"><br /><br />CONTENT<br />MODULES</a><a href=\"#\" onclick=\"LoadRModules();return false;\" class=\"schem schem-left-right\"><br /><br />RIGHT<br />MODULES</a></div><a href=\"#\" onclick=\"LoadFooters();return false;\" class=\"schem schem-header-top-footer\"><br />FOOTER<br /><br /></a></div>");
}

function ShowFileTypeInterface() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    var output = "<a href=\"#\" title=\"Add New File Type\" class=\"button icon sprite-NewPage\" onclick=\"$('#addfiletype').dialog('open');return false;\" />";
    $('#ribbon').prepend(output);
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowFileTypeInterface();return false;\"><span class=\"icon sprite-Settings\"></span>Hosted File Types</a>");
    $.post("/lidocaine.ashx?f=LoadFileTypes", function (data) {
        var oJSON = eval("(" + data + ")");
        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"if(confirm('Are you sure you want to delete this file type?')) DeleteFileType('" + oJSON[i].FileTypeID + "'); return false;\" /><a href=\"#\" onclick=\"$('#EditFileTypeExtension').html('" + oJSON[i].Extension + "');$('#pagePath').val('" + oJSON[i].FileTypeID + "');$('#EditFileTypeMime').val('" + oJSON[i].Mime + "');$('#editfiletype').dialog('open');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"$('#EditFileTypeExtension').html('" + oJSON[i].Extension + "');$('#pagePath').val('" + oJSON[i].FileTypeID + "');$('#EditFileTypeMime').val('" + oJSON[i].Mime + "');$('#editfiletype').dialog('open');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span><span class=\"filetype-extension\">" + oJSON[i].Extension + "</span>" + oJSON[i].Mime + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function ShowUsersInterface() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    var output = "<a href=\"#\" title=\"Add New User\" class=\"button icon sprite-NewUser\" onclick=\"$('#adduser').dialog('open');return false;\" />";
    $('#ribbon').prepend(output);
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowUsersInterface();return false;\"><span class=\"icon sprite-Settings\"></span>Application Users</a>");
    $.post("/lidocaine.ashx?f=LoadUsers", function (data) {
        var oJSON = eval("(" + data + ")");
        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"if(confirm('Are you sure you want to delete this user?')) DeleteUser('" + oJSON[i].UserID + "'); return false;\" /><a href=\"#\" onclick=\"$('#EditUserName').val('" + oJSON[i].UserName + "');$('#pagePath').val('" + oJSON[i].UserID + "');$('#edituser').dialog('open');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"$('#EditUserName').val('" + oJSON[i].UserName + "');$('#pagePath').val('" + oJSON[i].UserID + "');$('#edituser').dialog('open');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-User\"></span>" + oJSON[i].UserName + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function DeleteUser(userID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=DeleteUser", { "userID": userID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        ShowUsersInterface();
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function EditUser(dialog) {
    $('#editUserValidation').html('');
    var uName = $("#EditUserName").val();
    var uPassword = $("#EditUserPassword").val();
    if (uName.length < 1) {
        $('#editUserValidation').append("A username cannot be blank.");
    }

    if (uPassword.length < 1) {
        $('#editUserValidation').append("A password cannot be blank.");
    }

    if ($('#newUserValidation').html() == '') {
        $(".loading").show();
        $.post("/lidocaine.ashx?f=UpdateUser", { "userID": $("#pagePath").val(), "username": uName, "password": uPassword }, function (data) {
            dialog.dialog('close');
            $("#EditUserName").html("");
            $("#EditUserPassword").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            ShowUsersInterface();
            $('.loading').hide();
        }).error(function (data) {
            handleError(data);
        });
    }
}

function AddUser(dialog) {
    $('#newUserValidation').html('');
    var uName = $("#NewUserName").val();
    var uPassword = $("#NewUserPassword").val();
    if (uName.length < 1) {
        $('#newUserValidation').append("A new username cannot be blank.");
    }
    
    if (uPassword.length < 1) {
        $('#newUserValidation').append("A new password cannot be blank.");
    }

    if ($('#newUserValidation').html() == '') {
        $(".loading").show();
        $.post("/lidocaine.ashx?f=AddUser", { "username": uName, "password": uPassword }, function (data) {
            dialog.dialog('close');
            $("#NewUserName").val("");
            $("#NewUserPassword").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            ShowUsersInterface();
            $('.loading').hide();
        }).error(function (data) {
            handleError(data);
        });
    }
}

function DeleteFileType(filetypeID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=DeleteFileType", { "filetypeID": filetypeID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        ShowFileTypeInterface();
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function EditFileType(dialog) {
    $('#editFileTypeValidation').html('');
    var ftMime = $("#EditFileTypeMime").val();
    if (ftMime.length < 1) {
        $('#editFileTypeValidation').append("A file type MIME encoding cannot be blank.");
    }
    else {
        $(".loading").show();
        $.post("/lidocaine.ashx?f=UpdateFileType", { "filetypeID": $("#pagePath").val(), "mime": mime }, function (data) {
            dialog.dialog('close');
            $("#EditFileTypeExtension").html("");
            $("#EditFileTypeMime").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            ShowFileTypeInterface();
            $('.loading').hide();
        }).error(function (data) {
            handleError(data);
        });
    }
}

function AddFileType(dialog) {
    $('#newFileTypeValidation').html('');
    var ftExtension = $("#NewFileTypeExtension").val();
    var ftMime = $("#NewFileTypeMime").val();
    if (ftExtension.length < 1) {
        $('#newFileTypeValidation').append("A new file type extension cannot be blank.");
    }
    if (ftMime.length < 1) {
        $('#newFileTypeValidation').append("A new file type MIME encoding cannot be blank.");
    }
    else if (!ftExtension.match(/^[\w]+$/)) {
        $('#newFileTypeValidation').append("Please use only alphanumeric characters for the file type extension.");
    }
    
    if ($('#newFileTypeValidation').html() == '') {
        $(".loading").show();
        $.post("/lidocaine.ashx?f=AddFileType", { "extension": extension, "mime": mime }, function (data) {
            dialog.dialog('close');
            $("#NewFileTypeExtension").val("");
            $("#NewFileTypeMime").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            ShowFileTypeInterface();
            $('.loading').hide();
        }).error(function (data) {
            handleError(data);
        });
    }
}

function UpdatePageSettingsTitle(pageID, pageTitle) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=UpdatePageSettingsTitle", { "title": pageTitle, "pageID": pageID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        EditPageSettings($('#pagePath').val());
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function UpdatePageSettingsHeader(pageID, headerID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=UpdatePageSettingsHeader", { "headerID": headerID, "pageID": pageID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        EditPageSettings($('#pagePath').val());
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function UpdatePageSettingsFooter(pageID, footerID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=UpdatePageSettingsFooter", { "footerID": footerID, "pageID": pageID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        EditPageSettings($('#pagePath').val());
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function UpdatePageSettingsCSS(pageID, cssID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsCSS",
        data: { "cssID": cssID, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function UpdatePageSettingsJS(pageID, jsID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsJS",
        data: { "jsID": jsID, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangePageTopNavGroupItemOrder(oldOrder, newOrder, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsTopNavOrder",
        data: { "oldOrder": oldOrder, "newOrder": newOrder, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangePageLeftNavGroupItemOrder(oldOrder, newOrder, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsLeftNavOrder",
        data: { "oldOrder": oldOrder, "newOrder": newOrder, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangePageRightModuleOrder(oldOrder, newOrder, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsRightOrder",
        data: {"oldOrder": oldOrder, "newOrder": newOrder, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangePageMainModuleOrder(oldOrder, newOrder, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsMainOrder",
        data: {"oldOrder": oldOrder, "newOrder": newOrder, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeletePageTopNavItem(order, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsTopNavRemove",
        data: { "order": order, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeletePageLeftNavItem(order, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsLeftNavRemove",
        data: { "order": order, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddTopNavToPage(pageID, navName) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsTopNavAdd",
        data: { "pageID": pageID, "navName": navName },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddLeftNavToPage(pageID, navName) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsLeftNavAdd",
        data: { "pageID": pageID, "navName": navName },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeletePageRMod(order, pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsRightRemove",
        data: {"order": order, "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeletePageMMod(order, pageID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=UpdatePageSettingsMainRemove", { "order": order, "pageID": pageID }, function (data) {
        $('#ulValidation').html("<li>" + data + "</li>");
        EditPageSettings($('#pagePath').val());
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function AddRModToPage(pageID, moduleID, order) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsRightAdd",
        data: { "pageID": pageID, "moduleID": moduleID, "order": order },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddMModToPage(pageID, moduleID, order) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsMainAdd",
        data: { "pageID": pageID, "moduleID": moduleID, "order": order },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangePageVersion(pageID, contentID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsContentVersion",
        data: { "pageID": pageID, "blockID": contentID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditPageSettings(pagePath) {
    $('.loading').show();
    $('#pagePath').val(pagePath);
    $('#ribbon').children('a').remove();
    //$('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    //$('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditTopNav('" + navName + "');return false;\"><span class=\"icon sprite-Folder\"></span>" + navName + "</a>");
    $.post("/lidocaine.ashx?f=EditPageSettings", { "pagePath": pagePath }, function (data) {
        var oJSON = eval("(" + data + ")");
        var orderOptions = '';
        var output = '';

        output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldTitle\">Title:</label><input id=\"pageSettingsFieldTitle\" type=\"text\" value=\"" + oJSON[0].pTitle + "\" maxlength=\"500\" onchange=\"UpdatePageSettingsTitle('" + oJSON[0].pID + "', $(this).val()); return false;\" /></div>";

        orderOptions = "<option value=\"\"></option>";
        for (var q = 0; q < oJSON[0].Headers.length; q++) {
            if (oJSON[0].Headers[q].ID == oJSON[0].pHead) {
                orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].Headers[q].ID + "\">" + oJSON[0].Headers[q].Name + "</option>";
            }
            else {
                orderOptions += "<option value=\"" + oJSON[0].Headers[q].ID + "\">" + oJSON[0].Headers[q].Name + "</option>";
            }
        }
        output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldHeader\">Header:</label><select id=\"pageSettingsFieldHeader\" onchange=\"UpdatePageSettingsHeader('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

        orderOptions = "<option value=\"\"></option>";
        for (var r = 0; r < oJSON[0].Footers.length; r++) {
            if (oJSON[0].Footers[r].ID == oJSON[0].pFoot) {
                orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].Footers[r].ID + "\">" + oJSON[0].Footers[r].Name + "</option>";
            }
            else {
                orderOptions += "<option value=\"" + oJSON[0].Footers[r].ID + "\">" + oJSON[0].Footers[r].Name + "</option>";
            }
        }
        output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldFooter\">Footer:</label><select id=\"pageSettingsFieldFooter\" onchange=\"UpdatePageSettingsFooter('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

        orderOptions = "<option value=\"\"></option>";
        for (var s = 0; s < oJSON[0].CSSs.length; s++) {
            if (oJSON[0].CSSs[s].ID == oJSON[0].pCss) {
                orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].CSSs[s].ID + "\">" + oJSON[0].CSSs[s].Name + "</option>";
            }
            else {
                orderOptions += "<option value=\"" + oJSON[0].CSSs[s].ID + "\">" + oJSON[0].CSSs[s].Name + "</option>";
            }
        }
        output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldCSS\">Cascading Style Sheet:</label><select id=\"pageSettingsFieldCSS\" onchange=\"UpdatePageSettingsCSS('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

        orderOptions = "<option value=\"\"></option>";
        for (var t = 0; t < oJSON[0].JSs.length; t++) {
            if (oJSON[0].JSs[t].ID == oJSON[0].pJs) {
                orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].JSs[t].ID + "\">" + oJSON[0].JSs[t].Name + "</option>";
            }
            else {
                orderOptions += "<option value=\"" + oJSON[0].JSs[t].ID + "\">" + oJSON[0].JSs[t].Name + "</option>";
            }
        }
        output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldJS\">Javascript:</label><select id=\"pageSettingsFieldJS\" onchange=\"UpdatePageSettingsJS('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

        // Top Navs
        orderOptions = '';
        output += "<strong class=\"navs-table-title\">Top Navigation:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>Group</th><th>Action</th></tr></thead><tbody>";
        for (var i = 0; i < oJSON[0].pTopNavs.length; i++) {
            orderOptions = '';
            for (var j = 0; j < oJSON[0].pTopNavs.length; j++) {
                if (oJSON[0].pTopNavs[i].Order == j) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (j + 1) + "\">" + (j + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (j + 1) + "\">" + (j + 1) + "</option>";
                }
            }

            output += "<tr" + (i % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangePageTopNavGroupItemOrder('" + oJSON[0].pTopNavs[i].Order + "', $(this).val(), '" + oJSON[0].pID + "'); return false;\">" + orderOptions + "</select></td><td>" + oJSON[0].pTopNavs[i].Name + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeletePageTopNavItem('" + oJSON[0].pTopNavs[i].Order + "', '" + oJSON[0].pID + "'); return false;\" /></td></tr>";
        }
        orderOptions = '';
        for (var u = 0; u < oJSON[0].TopNavs.length; u++) {
            orderOptions += "<option value=\"" + oJSON[0].TopNavs[u].Name + "\">" + oJSON[0].TopNavs[u].Name + "</option>";
        }
        output += "</tbody><tfoot><tr class=\"navs-table-header\"><td colspan=\"2\"><label for=\"pageSettingsFieldTopNavs\">Top Navigation Groups: </label><select id=\"pageSettingsFieldTopNavs\">" + orderOptions + "</select></td><td><a href=\"#\" onclick=\"AddTopNavToPage('" + oJSON[0].pID + "', $('#pageSettingsFieldTopNavs').val());return false;\">Add Selected Top Navigation Group</a></td></tr></tfoot></table>";

        // Left Navs
        orderOptions = '';
        output += "<strong class=\"navs-table-title\">Left Navigation:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>Group</th><th>Action</th></tr></thead><tbody>";
        for (var k = 0; k < oJSON[0].pLeftNavs.length; k++) {
            orderOptions = '';
            for (var l = 0; l < oJSON[0].pLeftNavs.length; l++) {
                if (oJSON[0].pLeftNavs[k].Order == l) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (l + 1) + "\">" + (l + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (l + 1) + "\">" + (l + 1) + "</option>";
                }
            }

            output += "<tr" + (k % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangePageLeftNavGroupItemOrder('" + oJSON[0].pLeftNavs[k].Order + "', $(this).val(), '" + oJSON[0].pID + "'); return false;\">" + orderOptions + "</select></td><td>" + oJSON[0].pLeftNavs[k].Name + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeletePageLeftNavItem('" + oJSON[0].pLeftNavs[k].Order + "', '" + oJSON[0].pID + "'); return false;\" /></td></tr>";
        }
        orderOptions = '';
        for (var v = 0; v < oJSON[0].LeftNavs.length; v++) {
            orderOptions += "<option value=\"" + oJSON[0].LeftNavs[v].Name + "\">" + oJSON[0].LeftNavs[v].Name + "</option>";
        }
        output += "</tbody><tfoot><tr class=\"navs-table-header\"><td colspan=\"2\"><label for=\"pageSettingsFieldLeftNavs\">Left Navigation Groups: </label><select id=\"pageSettingsFieldLeftNavs\">" + orderOptions + "</select></td><td><a href=\"#\" onclick=\"AddLeftNavToPage('" + oJSON[0].pID + "', $('#pageSettingsFieldLeftNavs').val());return false;\">Add Selected Left Navigation Group</a></td></tr></tfoot></table>";

        // Right modules
        var counter = 1;
        orderOptions = '';
        output += "<strong class=\"navs-table-title\">Right Modules:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>Module</th><th>Action</th></tr></thead><tbody>";
        for (var m = 0; m < oJSON[0].pRMods.length; m++) {
            orderOptions = '';
            for (var n = 0; n < oJSON[0].pRMods.length; n++) {
                if (oJSON[0].pRMods[m].Order == (n + 1)) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (n + 1) + "\">" + (n + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (n + 1) + "\">" + (n + 1) + "</option>";
                }
            }

            output += "<tr" + (m % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangePageRightModuleOrder('" + oJSON[0].pRMods[m].Order + "', $(this).val(), '" + oJSON[0].pID + "'); return false;\">" + orderOptions + "</select></td><td>" + oJSON[0].pRMods[m].Name + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeletePageRMod('" + oJSON[0].pRMods[m].Order + "', '" + oJSON[0].pID + "'); return false;\" /></td></tr>";
            counter++;
        }
        orderOptions = '';
        for (var w = 0; w < oJSON[0].RMods.length; w++) {
            orderOptions += "<option value=\"" + oJSON[0].RMods[w].ID + "\">" + oJSON[0].RMods[w].Name + "</option>";
        }
        output += "</tbody><tfoot><tr class=\"navs-table-header\"><td colspan=\"2\"><label for=\"pageSettingsFieldRMods\">Right Modules: </label><select id=\"pageSettingsFieldRMods\">" + orderOptions + "</select></td><td><a href=\"#\" onclick=\"AddRModToPage('" + oJSON[0].pID + "', $('#pageSettingsFieldRMods').val(), '" + counter + "');return false;\">Add Selected Right Module</a></td></tr></tfoot></table>";

        // Main modules
        counter = 1;
        orderOptions = '';
        output += "<strong class=\"navs-table-title\">Main Modules:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>Module</th><th>Action</th></tr></thead><tbody>";
        for (var o = 0; o < oJSON[0].pMMods.length; o++) {
            orderOptions = '';
            for (var p = 0; p < oJSON[0].pMMods.length; p++) {
                if (oJSON[0].pMMods[o].Order == (p + 1)) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (p + 1) + "\">" + (p + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (p + 1) + "\">" + (p + 1) + "</option>";
                }
            }

            output += "<tr" + (o % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangePageMainModuleOrder('" + oJSON[0].pMMods[o].Order + "', $(this).val(), '" + oJSON[0].pID + "'); return false;\">" + orderOptions + "</select></td><td>" + oJSON[0].pMMods[o].Name + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeletePageMMod('" + oJSON[0].pMMods[o].Order + "', '" + oJSON[0].pID + "'); return false;\" /></td></tr>";
            counter++;
        }
        orderOptions = '';
        for (var x = 0; x < oJSON[0].MMods.length; x++) {
            orderOptions += "<option value=\"" + oJSON[0].MMods[x].ID + "\">" + oJSON[0].MMods[x].Name + "</option>";
        }
        output += "</tbody><tfoot><tr class=\"navs-table-header\"><td colspan=\"2\"><label for=\"pageSettingsFieldMMods\">Main Modules: </label><select id=\"pageSettingsFieldMMods\">" + orderOptions + "</select></td><td><a href=\"#\" onclick=\"AddMModToPage('" + oJSON[0].pID + "', $('#pageSettingsFieldMMods').val(), '" + counter + "');return false;\">Add Selected Main Module</a></td></tr></tfoot></table>";

        // Page versions
        output += "<strong class=\"navs-table-title\">Content Versions:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Current Version</th><th>Created Date</th><th>Author</th><th></th></tr></thead><tbody>";
        for (var y = 0; y < oJSON[0].pVersions.length; y++) {
            output += "<tr" + (y % 2 == 0 ? " class=\"alt\"" : "") + "><td><input type=\"radio\"" + (oJSON[0].pVersions[y].ID == oJSON[0].pCurrentVersion ? " checked=\"checked\"" : "") + " name=\"pageSettingsVersion\" onclick=\"ChangePageVersion('" + oJSON[0].pID + "', '" + oJSON[0].pVersions[y].ID + "')\" /></td><td>" + oJSON[0].pVersions[y].CreatedDate + "</td><td>" + oJSON[0].pVersions[y].Author + "</td><td><a target=\"_blank\" href=\"/" + pagePath.replace(".aspx", ".preview") + "?HContentID=" + oJSON[0].pVersions[y].ID + "\">Preview</a></td></tr>";
        }
        output += "</tbody></table>";

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function SaveAndPublishPage(pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=SaveAndPublishPage",
        data: { "content": CKEDITOR.instances["pageEditor"].getData(), "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function SavePage(pageID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=SavePage",
        data: { "content": CKEDITOR.instances["pageEditor"].getData(), "pageID": pageID },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditPage(pagePath, pageName) {
    $('.loading').show();
    $('#pagePath').val(pagePath);
    $('#ribbon').children('a').remove();
    //$('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    //$('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditPage('" + pagePath + "', '" + pageName + "');return false;\"><span class=\"icon sprite-Edit\"></span>" + pageName + "</a>");
    $.post("/lidocaine.ashx?f=EditPage", { "pagePath": pagePath }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Save\" class=\"button icon sprite-Save\" onclick=\"SavePage('" + oJSON[0].PageID + "'); return false;\" />";
        if (oJSON[0].IsAdmin) {
            output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-SavePublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishPage('" + oJSON[0].PageID + "'); } return false;\" />";
        }
        $('#ribbon').prepend(output);
        output = '';
        output += "<br /><div id=\"pageEditor\">" + oJSON[0].Content + "</div>";
        $('#explorer').html(output);
        if (CKEDITOR.instances["pageEditor"]) {
            CKEDITOR.instances["pageEditor"].destroy(true);
        }

        CKEDITOR.replace('pageEditor');
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadDirectory(path) {
    $('.loading').show();
    $('#path').val(path);
    $.post("/lidocaine.ashx?f=LoadExplorer", { "path": (path == null ? "" : path), "depth": (path == null ? "0" : path.split('/').length) }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = '';
        if (oJSON[0].IsAdmin) {
            $('.template-link').show();
            output += "<a href=\"#\" title=\"Add New Folder\" class=\"button icon sprite-NewFolder\" onclick=\"$('#addfolder').dialog('open'); return false;\" /><a href=\"#\" title=\"Add New Page\" class=\"button icon sprite-NewPage\" onclick=\"$('#addpage').dialog('open'); return false;\" /><a href=\"#\" title=\"Upload File(s)\" class=\"button icon sprite-Download\" onclick=\"$('#addfile').dialog('open'); return false;\" />";
        }

        output += "<div class=\"breadcrumbs\"><a class=\"address\" href=\"#\" onclick=\"LoadDirectory();return false;\"><span class=\"icon sprite-Globe\"></span>" + oJSON[0].N + "</a>";
        if (path != null && path != '') {
            var nodes = path.split('/');
            var rpath = '';
            for (var i = 0; i < nodes.length; i++) {
                rpath += nodes[i];
                output += "<a class=\"address\" href=\"#\" onclick=\"LoadDirectory('" + rpath + "');return false;\"><span class=\"icon sprite-Folder\"></span>" + nodes[i] + "</a>";
                rpath += "/";
            }
        }

        output += "</div>";
        $('#ribbon').html(output);

        output = '';
        if (oJSON[0].IsAdmin) {
            for (var j = 0; j < oJSON[0].Items.length; j++) {
                //Source: 1=folder, 2=page, 3=file
                switch (oJSON[0].Items[j].Source) {
                    case "1":
                        output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-User\" onclick=\"permissionsFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Open\" class=\"button icon sprite-Open\" onclick=\"LoadDirectory('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Click To Open\" class=\"aFilename\" onclick=\"LoadDirectory('" + oJSON[0].Items[j].Path + "'); return false;\"><span class=\"icon sprite-Folder\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                    case "2":
                        output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deletePage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-User\" onclick=\"permissionsPage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renamePage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" onclick=\"EditPageSettings('" + oJSON[0].Items[j].Path + "');return false;\" title=\"Settings\" class=\"button icon sprite-Settings\" /><a href=\"#\" onclick=\"EditPage('" + oJSON[0].Items[j].Path + "', '" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" target=\"_blank\" class=\"button icon sprite-Preview\" title=\"View\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" target=\"_blank\" class=\"aFilename\" title=\"Click To View\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                    case "3":
                        output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-User\" onclick=\"permissionsFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" title=\"View\" class=\"button icon sprite-Preview\" target=\"_blank\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" title=\"Click To View\" class=\"aFilename\" target=\"_blank\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                }
            }
        }
        else {
            for (var k = 0; k < oJSON[0].Items.length; k++) {
                switch (oJSON[0].Items[k].Source) {
                    case "1":
                        output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Open\" class=\"button icon sprite-Open\" onclick=\"LoadDirectory('" + oJSON[0].Items[k].Path + "'); return false;\" /><a href=\"#\" title=\"Click To Open\" class=\"aFilename\" onclick=\"LoadDirectory('" + oJSON[0].Items[k].Path + "'); return false;\"><span class=\"icon sprite-Folder\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                    case "2":
                        output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"EditPage('" + oJSON[0].Items[k].Path + "', '" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" target=\"_blank\" class=\"button icon sprite-Preview\" title=\"View\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" target=\"_blank\" class=\"aFilename\" title=\"Click To View\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                    case "3":
                        output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"/" + oJSON[0].Items[k].Path + "\" title=\"View\" class=\"button icon sprite-Preview\" target=\"_blank\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" title=\"Click To View\" class=\"aFilename\" target=\"_blank\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                        break;
                }
            }
        }

        $('#explorer').html(output);
        $('.loading').hide();
        if (oJSON[0].IsAdmin) {
            LoadUsersAndGroups();
        }
    }).error(function (data) {
        handleError(data);
    });
}

function DeleteTopNavItem(itemID, navName, ordinal) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTopNavItem",
        data: { "itemID": itemID, "navName": navName, "ordinal": ordinal },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditTopNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangeTopNavGroupItemOrder(navName, itemID, oldOrdinal, newOrdinal) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateTopNavItemOrder",
        data: { "navName": navName, "itemID": itemID, "oldOrdinal": oldOrdinal, "newOrdinal": newOrdinal },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditTopNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditTopNav(navName) {
    $('.loading').show();
    $('#pagePath').val(navName);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditTopNav('" + navName + "');return false;\"><span class=\"icon sprite-Folder\"></span>" + navName + "</a>");
    $.post("/lidocaine.ashx?f=EditTopNav", { "navName": navName }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = '';
        output += "<a href=\"#\" title=\"Add Item\" class=\"button icon sprite-NewPage\" onclick=\"$('#addtopnavitem').dialog('open');return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this top navigation group:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this top navigation group.</strong><br />";
        }

        var orderOptions = '';
        output += "<table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>URL</th><th>Text</th><th>Action</th></tr></thead><tbody>";
        for (var j = 0; j < oJSON[0].Items.length; j++) {
            orderOptions = '';
            for (var k = 0; k < oJSON[0].Items.length; k++) {
                if (oJSON[0].Items[j].Order == (k + 1)) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (k + 1) + "\">" + (k + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (k + 1) + "\">" + (k + 1) + "</option>";
                }
            }

            output += "<tr" + (j % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangeTopNavGroupItemOrder('" + navName + "', '" + oJSON[0].Items[j].ID + "', '" + oJSON[0].Items[j].Order + "', $(this).val())\">" + orderOptions + "</select></td><td>" + oJSON[0].Items[j].URL + "</td><td>" + oJSON[0].Items[j].Text + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeleteTopNavItem('" + oJSON[0].Items[j].ID + "', '" + navName + "', '" + oJSON[0].Items[j].Order + "'); return false;\" /></td></tr>";
        }

        $('#itemType').val(oJSON[0].Items.length);

        output += "</tbody></table>";
        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function DeleteLeftNavItem(itemID, navName, ordinal) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteLeftNavItem",
        data: { "itemID": itemID, "navName": navName, "ordinal": ordinal},
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditLeftNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function ChangeLeftNavGroupItemOrder(navName, itemID, oldOrdinal, newOrdinal) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateLeftNavItemOrder",
        data: { "navName": navName, "itemID": itemID, "oldOrdinal": oldOrdinal, "newOrdinal": newOrdinal },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            EditLeftNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditLeftNav(navName) {
    $('.loading').show();
    $('#pagePath').val(navName);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditLeftNav('" + navName + "');return false;\"><span class=\"icon sprite-Folder\"></span>" + navName + "</a>");
    $.post("/lidocaine.ashx?f=EditLeftNav", { "navName": navName }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = '';
        output += "<a href=\"#\" title=\"Add Item\" class=\"button icon sprite-NewPage\" onclick=\"$('#addleftnavitem').dialog('open');return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this left navigation group:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this left navigation group.</strong><br />";
        }

        //left nav table.
        var orderOptions = '';
        output += "<table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Order</th><th>URL</th><th>Text</th><th>Action</th></tr></thead><tbody>";
        for (var j = 0; j < oJSON[0].Items.length; j++) {
            orderOptions = '';
            for (var k = 0; k < oJSON[0].Items.length; k++) {
                if (oJSON[0].Items[j].Order == (k + 1)) {
                    orderOptions += "<option selected=\"selected\" value=\"" + (k + 1) + "\">" + (k + 1) + "</option>";
                }
                else {
                    orderOptions += "<option value=\"" + (k + 1) + "\">" + (k + 1) + "</option>";
                }
            }

            output += "<tr" + (j % 2 == 0 ? " class=\"alt\"" : "") + "><td><select onchange=\"ChangeLeftNavGroupItemOrder('" + navName + "', '" + oJSON[0].Items[j].ID + "', '" + oJSON[0].Items[j].Order + "', $(this).val())\">" + orderOptions + "</select></td><td>" + oJSON[0].Items[j].URL + "</td><td>" + oJSON[0].Items[j].Text + "</td><td><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"DeleteLeftNavItem('" + oJSON[0].Items[j].ID + "', '" + navName + "', '" + oJSON[0].Items[j].Order + "'); return false;\" /></td></tr>";
        }

        $('#itemType').val(oJSON[0].Items.length);

        output += "</tbody></table>";
        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadLeftNav() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadLeftNav();return false;\"><span class=\"icon sprite-Folder\"></span>Left Navigation Groups</a>");
    $.post("/lidocaine.ashx?f=LoadLeftNav", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Register New Left Navigation Group\" class=\"button icon sprite-NewPage\" onclick=\"$('#addleftnav').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"EditLeftNav('" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditLeftNav('" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadTopNav() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadTopNav();return false;\"><span class=\"icon sprite-Folder\"></span>Top Navigation Groups</a>");
    $.post("/lidocaine.ashx?f=LoadTopNav", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Register New Top Navigation Group\" class=\"button icon sprite-NewPage\" onclick=\"$('#addtopnav').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"EditTopNav('" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditTopNav('" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadRModules() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadRModules();return false;\"><span class=\"icon sprite-Folder\"></span>Right Modules</a>");
    $.post("/lidocaine.ashx?f=LoadRModules", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Register New Right Module\" class=\"button icon sprite-NewPage\" onclick=\"$('#addrmodule').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteRModule('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" onclick=\"describeRModule('" + oJSON[i].ID + "');return false;\" title=\"View Description\" class=\"button icon sprite-Preview\" /><a href=\"#\" onclick=\"describeRModule('" + oJSON[i].ID + "');return false;\" title=\"Click To View Description\" class=\"aFilename\"><span class=\"icon sprite-New\"></span><strong>" + oJSON[i].Name + "</strong> - " + oJSON[i].Path + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadMModules() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadMModules();return false;\"><span class=\"icon sprite-Folder\"></span>Main Modules</a>");
    $.post("/lidocaine.ashx?f=LoadMModules", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Register New Main Module\" class=\"button icon sprite-NewPage\" onclick=\"$('#addmmodule').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteMModule('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" onclick=\"describeMModule('" + oJSON[i].ID + "');return false;\" title=\"View Description\" class=\"button icon sprite-Preview\" /><a href=\"#\" onclick=\"describeMModule('" + oJSON[i].ID + "');return false;\" title=\"Click To View Description\" class=\"aFilename\"><span class=\"icon sprite-New\"></span><strong>" + oJSON[i].Name + "</strong> - " + oJSON[i].Path + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function SaveAndPublishHeader() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateHeader",
        data: { "content" : CKEDITOR.instances["headerEditor"].getData(), "headerID" : $('#pagePath').val() },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditHeader(headerID, headerName) {
    $('.loading').show();
    $('#pagePath').val(headerID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditHeader('" + headerID + "', '" + headerName + "');return false;\"><span class=\"icon sprite-New\"></span>" + headerName + "</a>");
    $.post("/lidocaine.ashx?f=EditHeader", { "headerID": headerID }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-Save\" onclick=\"SaveHeader(); return false;\" />";*/ // There is no versioning for headers, so this is not applicable.
        output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-SavePublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishHeader(); } return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this header:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this header.</strong><br />";
        }

        output += "<br /><div id=\"headerEditor\">" + oJSON[0].Content + "</div>";
        $('#explorer').html(output);
        if (CKEDITOR.instances["headerEditor"]) {
            CKEDITOR.instances["headerEditor"].destroy(true);
        }

        CKEDITOR.replace('headerEditor');
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadHeaders() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadHeaders();return false;\"><span class=\"icon sprite-Folder\"></span>Headers</a>");
    $.post("/lidocaine.ashx?f=LoadHeaders", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Add New Header\" class=\"button icon sprite-NewPage\" onclick=\"$('#addheader').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteHeader('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function SaveAndPublishFooter() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateFooter",
        data: { "content": CKEDITOR.instances["footerEditor"].getData(), "footerID": $('#pagePath').val() },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditFooter(footerID, footerName) {
    $('.loading').show();
    $('#pagePath').val(footerID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditFooter('" + footerID + "', '" + footerName + "');return false;\"><span class=\"icon sprite-New\"></span>" + footerName + "</a>");
    $.post("/lidocaine.ashx?f=EditFooter", { "footerID": footerID }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-Save\" onclick=\"SaveFooter(); return false;\" />";*/ // There is no versioning for footers, so this is not applicable.
        output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-SavePublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishFooter(); } return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this footer:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this footer.</strong><br />";
        }

        output += "<br /><div id=\"footerEditor\">" + oJSON[0].Content + "</div>";
        $('#explorer').html(output);
        if (CKEDITOR.instances["footerEditor"]) {
            CKEDITOR.instances["footerEditor"].destroy(true);
        }

        CKEDITOR.replace('footerEditor');
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadFooters() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadFooters();return false;\"><span class=\"icon sprite-Folder\"></span>Footers</a>");
    $.post("/lidocaine.ashx?f=LoadFooters", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Add New Footer\" class=\"button icon sprite-NewPage\" onclick=\"$('#addfooter').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteFooter('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function SaveAndPublishCss() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateCss",
        data: { "content": codeMirrorObject.getValue(), "cssID": $('#pagePath').val() },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditCss(cssID, cssName) {
    $('.loading').show();
    $('#pagePath').val(cssID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditCss('" + cssID + "', '" + cssName + "');return false;\"><span class=\"icon sprite-New\"></span>" + cssName + "</a>");
    $.post("/lidocaine.ashx?f=EditCss", { "cssID": cssID }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-Save\" onclick=\"SaveCss(); return false;\" />";*/ // There is no versioning for scripts, so this is not applicable.
        output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-SavePublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishCss(); } return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this CSS script:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this CSS script.</strong><br />";
        }

        output += "<br /><textarea id=\"cssEditor\">" + oJSON[0].Content + "</textarea>";
        $('#explorer').html(output);
        codeMirrorObject = CodeMirror.fromTextArea(document.getElementById("cssEditor"), { mode: "css", lineNumbers: true });
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadCss() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadCss();return false;\"><span class=\"icon sprite-Folder\"></span>CSS Scripts</a>");
    $.post("/lidocaine.ashx?f=LoadCss", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Add New CSS Script\" class=\"button icon sprite-NewPage\" onclick=\"$('#addcss').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteCss('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function SaveAndPublishJs() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdateJs",
        data: { "content": codeMirrorObject.getValue(), "jsID": $('#pagePath').val() },
        success: function (data) {
            $('#ulValidation').html("<li>" + data + "</li>");
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function EditJs(jsID, jsName) {
    $('.loading').show();
    $('#pagePath').val(jsID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditJs('" + jsID + "', '" + jsName + "');return false;\"><span class=\"icon sprite-New\"></span>" + jsName + "</a>");
    $.post("/lidocaine.ashx?f=EditJs", { "jsID": jsID }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-Save\" onclick=\"SaveJs(); return false;\" />";*/ // There is no versioning for scripts, so this is not applicable.
        output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-SavePublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishJs(); } return false;\" />";
        $('#ribbon').prepend(output);
        output = '';
        for (var i = 0; i < oJSON[0].Pages.length; i++) {
            output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
        }

        if (output != '') {
            output = "<strong>Pages using this Javascript:</strong><br /><ul>" + output + "</ul>";
        }
        else {
            output = "<strong>There are no pages using this Javascript.</strong><br />";
        }

        output += "<br /><textarea id=\"jsEditor\">" + oJSON[0].Content + "</textarea>";
        $('#explorer').html(output);
        codeMirrorObject = CodeMirror.fromTextArea(document.getElementById("jsEditor"), { mode: "javascript", lineNumbers: true });
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadJs() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadJs();return false;\"><span class=\"icon sprite-Folder\"></span>Javascripts</a>");
    $.post("/lidocaine.ashx?f=LoadJs", function (data) {
        var oJSON = eval("(" + data + ")");
        var output = "<a href=\"#\" title=\"Add New Javascript\" class=\"button icon sprite-NewPage\" onclick=\"$('#addjs').dialog('open'); return false;\" />";
        $('#ribbon').prepend(output);

        output = '';
        for (var i = 0; i < oJSON.length; i++) {
            output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-Delete\" onclick=\"deleteJs('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-Rename\" onclick=\"renameJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-Edit\" /><a href=\"#\" onclick=\"EditJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"aFilename\"><span class=\"icon sprite-New\"></span>" + oJSON[i].Name + "</a></div>";
        }

        $('#explorer').html(output);
        $('.loading').hide();
    }).error(function (data) {
        handleError(data);
    });
}

function LoadUsersAndGroups() {
    $.get("/lidocaine.ashx?f=LoadUsers", function (data) {
        var oJSON = eval("(" + data + ")");
        var users = "";
        for (var i = 0; i < oJSON.length; i++) {
            users += "<option value=\"" + oJSON[i].UserID + "\">" + oJSON[i].UserName + "</option>";
        }
        $("#UserList").html(users);
    }).error(function (data) {
        handleError(data);
    });
    $.get("/lidocaine.ashx?f=LoadGroups", function (data) {
        var oJSON = eval("(" + data + ")");
        var groups = "";
        for (var i = 0; i < oJSON.length; i++) {
            groups += "<option value=\"" + oJSON[i].GroupID + "\">" + oJSON[i].GroupName + "</option>";
        }
        $("#GroupList").html(groups);
    }).error(function (data) {
        handleError(data);
    });
}

function RenameFile(dialog) {
    var fileName = $("#RenameFile").val();
    if (fileName.length < 1) {
        $('#renameFileValidation').html("A new file name cannot be blank.");
    }
    else if (!fileName.match(/^[.\w]+$/)) {
        $('#renameFileValidation').html("Please use only alphanumeric characters and \".\".");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameFile",
            data: "path=" + $('#path').val() + "&name=" + fileName + "&existingPath=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameFile").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenamePage(dialog) {
    var pageName = $("#RenamePage").val();
    if (pageName.length < 1) {
        $('#renamePageValidation').html("A new page name cannot be blank.");
    }
    else if (!pageName.match(/^[\w]+$/)) {
        $('#renamePageValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenamePage",
            data: "path=" + $('#path').val() + "&name=" + pageName + "&existingPath=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenamePage").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenameFolder(dialog) {
    var folderName = $("#RenameFolder").val();
    if (folderName.length < 1) {
        $('#renameFolderValidation').html("A new folder name cannot be blank.");
    }
    else if (!folderName.match(/^[\w]+$/)) {
        $('#renameFolderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameFolder",
            data: "path=" + $('#path').val() + "&name=" + folderName + "&existingPath=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameFolder").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenameHeader(dialog) {
    var headerName = $("#RenameHeader").val();
    if (headerName.length < 1) {
        $('#renameHeaderValidation').html("A new header name cannot be blank.");
    }
    else if (!headerName.match(/^[\w]+$/)) {
        $('#renameHeaderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameHeader",
            data: "name=" + headerName + "&existingName=" + $('#path').val() + "&headerID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameHeader").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadHeaders();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenameFooter(dialog) {
    var footerName = $("#RenameFooter").val();
    if (footerName.length < 1) {
        $('#renameFooterValidation').html("A new footer name cannot be blank.");
    }
    else if (!footerName.match(/^[\w]+$/)) {
        $('#renameFooterValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameFooter",
            data: "name=" + footerName + "&existingName=" + $('#path').val() + "&footerID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameFooter").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadFooters();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenameCss(dialog) {
    var cssName = $("#RenameCss").val();
    if (cssName.length < 1) {
        $('#renameCssValidation').html("A new CSS script name cannot be blank.");
    }
    else if (!cssName.match(/^[\w]+$/)) {
        $('#renameCssValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameCss",
            data: "name=" + cssName + "&existingName=" + $('#path').val() + "&scriptID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameCss").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadCss();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function RenameJs(dialog) {
    var jsName = $("#RenameJs").val();
    if (jsName.length < 1) {
        $('#renameJsValidation').html("A new Javascript name cannot be blank.");
    }
    else if (!jsName.match(/^[\w]+$/)) {
        $('#renameJsValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameJs",
            data: "name=" + jsName + "&existingName=" + $('#path').val() + "&scriptID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameJs").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadJs();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function DeleteFile(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteFile",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeletePage(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeletePage",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteFolder(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteFolder",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteRModule(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteRModule",
        data: "moduleID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadRModules();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteMModule(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteMModule",
        data: "moduleID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadMModules();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteHeader(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadHeaders();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteFooter(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadFooters();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteCss(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteCss",
        data: "scriptID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadCss();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function DeleteJs(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteJs",
        data: "scriptID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            LoadJs();
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddPage(dialog) {
    var pageName = $("#NewPage").val();
    if (pageName.length < 1) {
        $('#newPageValidation').html("A new page name cannot be blank.");
    }
    else if (!pageName.match(/^[\w]+$/)) {
        $('#newPageValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddPage",
            data: "path=" + $('#path').val() + "&name=" + pageName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewPage").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddFolder(dialog) {
    var folderName = $("#NewFolder").val();
    if (folderName.length < 1) {
        $('#newFolderValidation').html("A new folder name cannot be blank.");
    }
    else if (!folderName.match(/^[\w]+$/)) {
        $('#newFolderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddFolder",
            data: "path=" + $('#path').val() + "&name=" + folderName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewFolder").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddRModule(dialog) {
    var moduleName = $("#NewRModule").val();
    var modulePath = $("#NewRModulePath").val();
    var moduleDescription = $("#NewRModuleDescription").val();
    if (moduleName.length < 1) {
        $('#newRModuleValidation').html("A new right module name cannot be blank.");
    }
    else if (!moduleName.match(/^[ \w\.]+$/)) {
        $('#newRModuleValidation').html("Please use only alphanumeric characters.");
    }
    
    if (modulePath.length < 1) {
        $('#newMModuleValidation').html("A new main module path cannot be blank.");
    }

    if ($('#newMModuleValidation').html() == "") {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddRModule",
            data: { "name": moduleName, "path": modulePath, "description": moduleDescription },
            success: function (data) {
                dialog.dialog('close');
                $("#NewRModule").val("");
                $("#NewRModulePath").val("");
                $("#NewRModuleDescription").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadRModules();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddMModule(dialog) {
    var moduleName = $("#NewMModule").val();
    var modulePath = $("#NewMModulePath").val();
    var moduleDescription = $("#NewMModuleDescription").val();
    if (moduleName.length < 1) {
        $('#newMModuleValidation').html("A new main module name cannot be blank.");
    }
    else if (!moduleName.match(/^[ \w\.]+$/)) {
        $('#newMModuleValidation').html("Please use only alphanumeric characters.");
    }
    
    if (modulePath.length < 1) {
        $('#newMModuleValidation').html("A new main module path cannot be blank.");
    }

    if ($('#newMModuleValidation').html() == "") {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddMModule",
            data: { "name" : moduleName, "path" : modulePath, "description" : moduleDescription },
            success: function (data) {
                dialog.dialog('close');
                $("#NewMModule").val("");
                $("#NewMModulePath").val("");
                $("#NewMModuleDescription").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadMModules();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddHeader(dialog) {
    var headerName = $("#NewHeader").val();
    if (headerName.length < 1) {
        $('#newHeaderValidation').html("A new header name cannot be blank.");
    }
    else if (!headerName.match(/^[\w]+$/)) {
        $('#newHeaderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddHeader",
            data: "name=" + headerName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewHeader").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadHeaders();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddFooter(dialog) {
    var footerName = $("#NewFooter").val();
    if (footerName.length < 1) {
        $('#newFooterValidation').html("A new footer name cannot be blank.");
    }
    else if (!footerName.match(/^[\w]+$/)) {
        $('#newFooterValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddFooter",
            data: "name=" + footerName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewFooter").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadFooters();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddCss(dialog) {
    var cssName = $("#NewCss").val();
    if (cssName.length < 1) {
        $('#newCssValidation').html("A new CSS script name cannot be blank.");
    }
    else if (!cssName.match(/^[\w]+$/)) {
        $('#newCssValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddCss",
            data: "name=" + cssName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewCss").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadCss();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddJs(dialog) {
    var jsName = $("#NewJs").val();
    if (jsName.length < 1) {
        $('#newJsValidation').html("A new Javascript name cannot be blank.");
    }
    else if (!jsName.match(/^[\w]+$/)) {
        $('#newJsValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddJs",
            data: "name=" + jsName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewJs").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadJs();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddLeftNav(dialog) {
    var leftNavName = $("#NewLeftNav").val();
    if (leftNavName.length < 1) {
        $('#newLeftNavValidation').html("A new navigation group name cannot be blank.");
    }
    else if (!leftNavName.match(/^[\w]+$/)) {
        $('#newLeftNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddLeftNav",
            data: "name=" + leftNavName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewLeftNav").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadLeftNav();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddLeftNavItem(dialog) {
    $(".loading").show();
    var count = $('#itemType').val();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=AddLeftNavItem",
        data: { "text": $("#NewLeftNavItemText").val(), "link": $("#NewLeftNavItemURL").val(), "group": $("#pagePath").val(), "order": (parseInt(count) + 1) },
        success: function (data) {
            dialog.dialog('close');
            $("#NewLeftNavItemText").val("");
            $("#NewLeftNavItemURL").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            EditLeftNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddTopNav(dialog) {
    var topNavName = $("#NewTopNav").val();
    if (topNavName.length < 1) {
        $('#newTopNavValidation').html("A new navigation group name cannot be blank.");
    }
    else if (!topNavName.match(/^[\w]+$/)) {
        $('#newTopNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTopNav",
            data: "name=" + topNavName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewTopNav").val("");
                $('#ulValidation').html("<li>" + data + "</li>");
                LoadTopNav();
                $('.loading').hide();
            },
            error: function (data) {
                handleError(data);
            }
        });
    }
}

function AddTopNavItem(dialog) {
    $(".loading").show();
    var count = $('#itemType').val();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=AddTopNavItem",
        data: { "text": $("#NewTopNavItemText").val(), "link": $("#NewTopNavItemURL").val(), "group": $("#pagePath").val(), "order": (parseInt(count) + 1) },
        success: function (data) {
            dialog.dialog('close');
            $("#NewTopNavItemText").val("");
            $("#NewTopNavItemURL").val("");
            $('#ulValidation').html("<li>" + data + "</li>");
            EditTopNav($("#pagePath").val());
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function describeRModule(moduleID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=LoadModuleDescription", { "ID": moduleID }, function (data) {
        $('#describermodule').html(data);
        $('#describermodule').dialog('open');
        $(".loading").hide();
    }).error(function (data) {
        handleError(data);
    });
}
function describeMModule(moduleID) {
    $(".loading").show();
    $.post("/lidocaine.ashx?f=LoadModuleDescription", { "ID": moduleID }, function (data) {
        $('#describemmodule').html(data);
        $('#describemmodule').dialog('open');
        $(".loading").hide();
    }).error(function (data) {
        handleError(data);
    });
}
function deleteFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#deletefolder').dialog('open');
}
function deletePage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#deletepage').dialog('open');
}
function deleteFile(filePath) {
    $("#pagePath").val(filePath);
    $('#deletefile').dialog('open');
}
function deleteRModule(moduleID) {
    $("#pagePath").val(moduleID);
    $('#deletermodule').dialog('open');
}
function deleteMModule(moduleID) {
    $("#pagePath").val(moduleID);
    $('#deletemmodule').dialog('open');
}
function deleteHeader(headerID) {
    $("#pagePath").val(headerID);
    $('#deleteheader').dialog('open');
}
function deleteFooter(footerID) {
    $("#pagePath").val(footerID);
    $('#deletefooter').dialog('open');
}
function deleteCss(scriptID) {
    $("#pagePath").val(scriptID);
    $('#deletecss').dialog('open');
}
function deleteJs(scriptID) {
    $("#pagePath").val(scriptID);
    $('#deletejs').dialog('open');
}
function renameFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#renamefolder').dialog('open');
}
function renamePage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#renamepage').dialog('open');
}
function renameFile(filePath) {
    $("#pagePath").val(filePath);
    $('#renamefile').dialog('open');
}
function renameHeader(headerID, headerName) {
    $("#pagePath").val(headerID);
    $("#path").val(headerName);
    $('#renameheader').dialog('open');
}
function renameFooter(footerID, footerName) {
    $("#pagePath").val(footerID);
    $("#path").val(footerName);
    $('#renamefooter').dialog('open');
}
function renameCss(cssID, cssName) {
    $("#pagePath").val(cssID);
    $("#path").val(cssName);
    $('#renamecss').dialog('open');
}
function renameJs(jsID, jsName) {
    $("#pagePath").val(jsID);
    $("#path").val(jsName);
    $('#renamecss').dialog('open');
}
function permissionsFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#permissions').dialog('open');
    $("#itemType").val("2");
    LoadFolderPermissions();
}
function permissionsPage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#permissions').dialog('open');
    $("#itemType").val("0");
    LoadPagePermissions();
}
function permissionsFile(filePath) {
    $("#pagePath").val(filePath);
    $('#permissions').dialog('open');
    $("#itemType").val("1");
    LoadFilePermissions();
}

function LoadFolderPermissions() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=GetFolderPermissions",
        data: "FolderPath=" + $('#pagePath').val(),
        success: function (data) {
            var oJSON = eval("(" + data + ")");
            var strTable = "<table cellspacing=\"0\" class=\"permissionsTable\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Group Name</th><th>Action</th></tr></thead><tbody>";
            for (var i = 0; i < oJSON.length; i++) {
                strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 2);\">REMOVE</span></td></tr>";
            }
            strTable += "</tbody></table>";
            $("#permissionsDialogTable").html(strTable);
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function LoadPagePermissions() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=GetPagePermissions",
        data: "PagePath=" + $('#pagePath').val(),
        success: function (data) {
            var oJSON = eval("(" + data + ")");
            var strTable = "<table cellspacing=\"0\" class=\"permissionsTable\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Group Name</th><th>Action</th></tr></thead><tbody>";
            for (var i = 0; i < oJSON.length; i++) {
                strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 0);\">REMOVE</span></td></tr>";
            }
            strTable += "</tbody></table>";
            $("#permissionsDialogTable").html(strTable);
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function LoadFilePermissions() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=GetFilePermissions",
        data: "VirtualPath=" + $('#pagePath').val(),
        success: function (data) {
            var oJSON = eval("(" + data + ")");
            var strTable = "<table cellspacing=\"0\" class=\"permissionsTable\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Group Name</th><th>Action</th></tr></thead><tbody>";
            for (var i = 0; i < oJSON.length; i++) {
                strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 1);\">REMOVE</span></td></tr>";
            }
            strTable += "</tbody></table>";
            $("#permissionsDialogTable").html(strTable);
            $('.loading').hide();
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function AddPermissions(itemPath, allowType, allowID, itemType) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=AddPermissions",
        data: "AllowType=" + allowType + "&AllowID=" + allowID + "&ItemPath=" + itemPath,
        success: function () {
            if (itemType == 2) {
                LoadFolderPermissions();
            }
            else if (itemType == 0) {
                LoadPagePermissions();
            }
            else if (itemType == 1) {
                LoadFilePermissions();
            }
        },
        error: function (data) {
            handleError(data);
        }
    });
}

function RemovePermissions(permissionID, itemType) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=RemovePermissions",
        data: "PermissionID=" + permissionID,
        success: function () {
            if (itemType == 2) {
                LoadFolderPermissions();
            }
            else if (itemType == 0) {
                LoadPagePermissions();
            }
            else if (itemType == 1) {
                LoadFilePermissions();
            }
        },
        error: function (data) {
            handleError(data);
        }
    });
}